package com.hsxc.cdz.monitor.bean.extend.server.message;

import com.hsxc.cdz.base.util.BytesUtil;
import com.hsxc.cdz.mgmt.GlobalManager;
import com.hsxc.cdz.monitor.FSM.CachePile;
import com.hsxc.cdz.monitor.bean.extend.Msg;
import com.hsxc.cdz.monitor.bean.extend.server.content.BillSerContent;

import io.netty.channel.Channel;

/**
 * 服务器回复充电桩上传账单报文
 * @author 刘力文
 *
 */
public class BillSerMsg extends ServerMessage<BillSerContent>{

	/*****************************************************************/
	//构造方法
	public BillSerMsg(){
		this.setIsOpen(Msg.START_UP);
		this.setContentLength(BillSerContent.length);
	    this.setIsRegister(Msg.DID_REGISTER);
	    this.setFrame(ServerMessage.RESPONSE_BILL);
	    this.setContent(new BillSerContent());
	}
	
	public BillSerMsg(String pileAddress,String lineNum,String response){
		this();
		this.setAddress(pileAddress);
		this.setLineNum(lineNum);
		this.setResponse(response);
	}
	/*****************************************************************/
	//重写父类方法
	@Override
	public boolean execute() {
		CachePile cachePile =
				GlobalManager.pileManager.getPile(this.getAddress());
		if(cachePile==null){
			return false;
		}
		this.setOperator(cachePile.getOperator());
		Channel channel = cachePile.getChannel();
		if(channel==null){
			return false;
		}
		channel.writeAndFlush(this);
		return true;
	}

	/*****************************************************************/
	//重写父类方法
	public byte[] getByte(){
		return BytesUtil.concat(super.getByte()
				, getContent().getByte());
	}

}
